package net;

import java.net.InetAddress;

/**
 * InetAddress API
 */
public class InetAddressAPI {

    public static void main(String[] args) throws Exception {
        /*
            1.获取本机InetAddress对象 getLocalHost
            2.根据指定主机名/域名获取ip地址对象 getByName
            3.获取InetAddress对象的主机名 getHostName
            4.获取InetAddress对象的住址 getHostAddress
         */

        //获取本机 InetAddress 对象 getLocalHost
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//    LAPTOP-08JTEP7F/192.168.1.102

        //根据指定主机名/域名获取ip地址对象 getByName
        InetAddress host2 = InetAddress.getByName("LAPTOP-08JTEP7F");
        System.out.println(host2);

        InetAddress host3 = InetAddress.getByName("www.baidu.com");
        System.out.println(host3);

        //获取InetAddress 对象的主机名
        String host3Name = host3.getHostName();
        System.out.println(host3Name);

        //获取InetAddress对象的地址 getHostAddress
        String host3Address = host3.getHostAddress();
        System.out.println(host3Address);
    }
}
